<template>
  <view>
    <cu-custom
      bgColor="bg-white"
      :isBack="true"
    >
      <block slot="backText">返回</block>
      <block slot="content">修改密码</block>
    </cu-custom>
    <view class="forget">
      <view class="content">
        
        <view class="main">
          <view class="tips">若您忘记了密码，可在此重置新密码。</view>
         <wInput
            v-model="phoneData"
            type="text"
            maxlength="11"
            placeholder="手机号码"
            isDisabled
			style="background-color:#E5E5E5;"
           ></wInput>
          <!-- <wInput
              v-model="mailData"
              type="text"
              maxlength="40"
              placeholder="请输入邮件"
          ></wInput> -->
          <wInput
            v-model="passData"
            type="password"
            maxlength="32"
            placeholder="请输入新密码"
            isShowPass
			autocomplete="off"
          ></wInput>

          <!-- <wInput
            v-model="verCode"
            type="number"
            maxlength="6"
            placeholder="验证码"
            isShowCode
            codeText="获取重置码"
            setTime="60"
            ref="runCode"
            @setCode="getVerCode()"
          ></wInput> -->
        </view>

        <wButton
          text="修改密码"
          :rotate="isRotate"
          @click.native="startRePass()"
        ></wButton>

      </view>
    </view>
  </view>
</template>

<script>
import { toLogin,quit } from "@/server/api.js";
import {
  phonePwdUpdateApi,
  memberInfoApi
} from "@/server/config.js";
import wInput from "@/components/watch-login/watch-input.vue"; //input
import wButton from "@/components/watch-login/watch-button.vue"; //button
export default {
  onShow() {
    toLogin();
  },
  data() {
    return {
      // logoImage: "https://imgt1.oss-cn-shanghai.aliyuncs.com/ecAllRes/images/logo.png",
	  logoImage: "",
      phoneData: "", //电话
	  phoneReg:/^(13[0-9]|14[01456879]|15[0-3,5-9]|16[2567]|17[0-8]|18[0-9]|19[0-3,5-9])d{8}$/,
      reg: /^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/ ,
	  id:null,
      mailData: "", //邮箱
      passData: "", //密码
      verCode: "", //验证码
      isRotate: false //是否加载旋转
    };
  },
  components: {
    wInput,
    wButton
  },
  onLoad() {
    this.getMemberInfo();
  },
  methods: {
    async getMemberInfo() {
      const data = await memberInfoApi({});
      this.phoneData = data.data.phoneNumber;
	  this.id = data.data.id;
    },
    
    async getVerCode() {
      //获取验证码
      // if (this.phoneData.length != 11) {
      //   uni.showToast({
      //     icon: "none",
      //     position: "bottom",
      //     title: "手机号不正确"
      //   });
      //   return false;
      // }
      if (!this.reg.test(this.mailData)) {
        uni.showToast({
          icon: "none",
          position: "bottom",
          title: "邮箱格式不正确"
        });
        return false;
      }
      const data = await emailPwdSendSmsApi({
        mail: this.mailData,
        user_name: this.phoneData,
      });

      console.log("获取验证码");
      if (data.data.res === false) {
        //模拟倒计时触发
        uni.showToast({
          icon: "none",
          position: "bottom",
          title: data.data.msg
        });
      }
      if (data.data.res === true) {
        //模拟倒计时触发
        this.$refs.runCode.$emit("runCode"); //触发倒计时（一般用于请求成功验证码后调用）
        uni.showToast({
          icon: "none",
          position: "bottom",
          title: "获取验证码成功"
        });
      }

      // setTimeout(function() {
      //   this.$refs.runCode.$emit("runCode", 0); //假装模拟下需要 终止倒计时
      //   // uni.showToast({
      //   //     icon: 'none',
      //   // 	position: 'bottom',
      //   //     title: '模拟倒计时终止'
      //   // });
      // }, 3000);
    },
    async startRePass() {
      //重置密码
      if (this.isRotate) {
        //判断是否加载中，避免重复点击请求
        return false;
      }
      // if (this.phoneData.length != 11) {
      //   uni.showToast({
      //     icon: "none",
      //     position: "bottom",
      //     title: "手机号不正确"
      //   });
      //   return false;
      // }
      if (this.passData.length < 6) {
        uni.showToast({
          icon: "none",
          position: "bottom",
          title: "密码不正确"
        });
        return false;
      }

      const data = await phonePwdUpdateApi({
        id: this.id,
        password: this.passData,
      });
      if(data.data.res === false){
        uni.showToast({
          title: data.data.msg,
          icon: "none",
          duration: 1500
        });
        return false;
      }
	  
      uni.showToast({
          title: "密码修改成功",
          icon: "none",
          duration: 1500
        });
      // uni.clearStorageSync(); 
      quit();
        setTimeout(function() {
          uni.navigateTo({
            url: "/apiPam/logincommon/main"
          });
        }, 1500);

      // this.isRotate=true
      // setTimeout(function(){
      // 	this.isRotate=false
      // },3000)
    }
  }
};
</script>
<style lang='scss' scoped>
@import url("../../components/watch-login/css/icon.css");
@import "./style";
</style>

